<template>
    <section>
        <!--工具条-->
        <div class="section-content clearfix">
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filters" class="companyui">
                    <el-form-item>
                        <!-- <el-input v-model="filters.name" placeholder="模板名称" @keyup.enter.native="getUsers()"></el-input> -->
                        <el-select v-model="filters.companyId" placeholder="组织" @change="getUsers">
                             <el-option label="全部" value=" "></el-option>
                            <el-option v-for="item in firmlist" :label="item.name" :value="item.companyId"></el-option>
                        </el-select>
                    </el-form-item>
                    <!-- <el-form-item class="tool-bt">
                        <el-button type="primary" v-on:click="getUsers">查询</el-button>
                    </el-form-item> -->
                    <el-form-item >                     
                        <el-button type="primary" @click="handleAdd">新增</el-button>
                    </el-form-item>
                </el-form>
            </el-col>

            <!--列表-->
            <h2>{{companyuiName}}</h2>
            <el-table :data="query" highlight-current-row style="width: 100%;">
                <!-- <el-table-column type="selection">
                </el-table-column> -->
                <el-table-column type="index" align="center" label="序号"  width="80":index="indexMethod">
                </el-table-column>
                <el-table-column align="center" prop="companyName" label="组织名称">
                </el-table-column>
                <el-table-column prop="updateTime" align="center" label="更新时间">
                </el-table-column>
                <el-table-column align="center" label="操作">
                    <template scope="scope">
                        <!-- <i title="编辑" @click="handleEdit(scope.$index, scope.row)" icon="el-icon-edit" circle class="pb-icon icon-edit"></i> -->
                        <i title="删除" @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete" circle class="pb-icon icon-delete"></i>
                    </template>
                </el-table-column>
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                    style="float:right;">
                </el-pagination>
            </el-col>
        </div>

        <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" v-model="addFormVisible" @close="addFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form @submit.native.prevent :model="addForm" label-width="100px" ref="addForm">
                    <el-form-item label="选择组织">
                       <el-select v-model="addForm.companyId" placeholder="组织">
                            <el-option v-for="item in firmlist" :label="item.name" :value="item.companyId"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import {
    device_template_ui_company_query,
    device_template_ui_company_post,
    device_template_ui_company_delete,
    user_querycompany
} from '../../api/api'
export default {
    data(){
        return{
            total: 0,
            page:'1',
            query:[],
            companyuiName:'',
            addFormVisible: false,
            addLoading: false,
            firmlist:[],
            filters:{
                companyId:''
            },
            addForm:{
                companyId:'',
                companyuiId:''
            },
        }
    },
    methods:{
        getUsers(){
            let para = {
                // pageNo: this.page,
                pageSize: 10,
                companyId: this.filters.companyId,
                uiId: this.addForm.companyuiId
            }
            device_template_ui_company_query(para).then(res => {
                console.log(res)
                if(res.code == 0){
                    this.query = res.data.list
                }
            })
        },
        handleAdd(){
            this.addFormVisible = true;
        },
        handleCurrentChange(val){
            this.page = val;
            this.getUsers();
        },
        indexMethod(index) {
            let num = (this.page - 1) * 10 + index + 1;
            return num;
        },
        addSubmit(){
            let para = {
                companyId: this.addForm.companyId,
                uiId: this.addForm.companyuiId,
            }
            device_template_ui_company_post(para).then(res => {
                if(res.code == 0){
                    this.$message({
                        message: '提交成功',
                        type: 'success'
                    });
                    this.addFormVisible = false;
                    this.getUsers();
                }
            })
        },
        //删除
        handleDel(index,row){
            this.$confirm('确认删除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    let para = {
                        id: row.id
                    }
                    device_template_ui_company_delete(para).then(res => {
                        if(res.code == 0){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getUsers();
                        }
                    })
                }).catch(() => {

                });
        },
    },
    mounted(){
        if(sessionStorage.getItem('companyuiName')){
            this.companyuiName = sessionStorage.getItem('companyuiName')
        }
        if(sessionStorage.getItem('companyuiId')){
            this.addForm.companyuiId = sessionStorage.getItem('companyuiId')
        }
        this.getUsers()
        let para = {
            pageNo: 1,
            pageSize: 9999,
            type: 1
        };
        user_querycompany(para).then(res => {
            this.begCode(res.code, res.message);
            if(res.code == 0){
                this.firmlist = res.data.list;
            }
        });
    }
}
</script>

<style>
 .companyui .el-input .el-input__inner{
     border-radius: 4px;
 }
</style>